# Witch hunts? Electoral cycles and corruption lawsuits in Argentina
# G Feierherd, E Gonzalez Ocantos, G Tuñón
# sessionInfo(): R version 4.3.0 (2023-04-21)

# Clear Workspace
rm(list=ls())

# load/install packages/functions  
if (!require("pacman")) install.packages("pacman")
pacman::p_load(
  ggplot2,
  estimatr,
  texreg,
  xtable,
  sandwich,
  dplyr,
  janitor, 
  gridExtra,
  grid,
  zoo,
  lubridate,
  tidyverse,
  gsheet,
  scales,
  rdrobust,
  rstatix,
  wesanderson,
  kableExtra
)

# Function to Extract RD Coefficients
extract_rd <- function(x){
  out <- c(
    as.character(round(x$coef[1], 2)),
    paste0("[", round(x$ci[3,1], 2), ":", round(x$ci[3,2], 2), "]"),
    as.character(round(x$pv[3], 3)),
    paste0(x$N_h[1], " | ", x$N_h[2]),
    as.character(round(x$bws[1,1], 1))
  )
  return(out)
}

# Display Options
options(digits=4, 
        scipen=100, 
        show.signif.stars=FALSE,
        max.print=2000,
        tibble.width=Inf,
        tibble.print_max=Inf,
        tibble.print_min=1)

# Set Working Folder
home <- ""

# Load Data
load(paste0(home, "witch_hunts_data.Rda"))

# Set Saving Folder
figures <- paste0(home,"figures/")  

# Set Graphical Parameters
ylim <- c(-.2, .6)
jit1 <- .75
bar1 <- .4
col_movie <- "Zissou1"
col_bin <- wes_palette(col_movie)

# Set Global Parameters
bw_max <- 37
bw_min <- 5
ylim <- c(-.2, .6)

# Table 1
tab1 <- dtaall %>%
  select(year, acc_K, acc_claim_K, acc_M, acc_claim_M, n) %>% 
  group_by(year) %>% 
  summarize_all(.funs = "sum")  %>% 
  dplyr::rename(
    Year = year,
    `Cases against Kirchnerism` = acc_K,
    `Cases filed by Kirchnerism` = acc_claim_K,
    `Cases against Macrism` = acc_M,
    `Cases filed by Macrism` = acc_claim_M,
    `Total cases` = n
  )

total <- c(
  "Total", 
  paste0(
    sum(tab1$`Cases against Kirchnerism`),
    " (",
    round(100 * sum(tab1$`Cases against Kirchnerism`) / sum(tab1$`Total cases`), 1),
    "%)"
  ),
  paste0(
    sum(tab1$`Cases filed by Kirchnerism`),
    " (",
    round(100 * sum(tab1$`Cases filed by Kirchnerism`) / sum(tab1$`Total cases`), 1),
    "%)"
  ),
  paste0(
    sum(tab1$`Cases against Macrism`),
    " (",
    round(100 * sum(tab1$`Cases against Macrism`) / sum(tab1$`Total cases`), 1),
    "%)"
  ),
  paste0(
    sum(tab1$`Cases filed by Macrism`),
    " (",
    round(100 * sum(tab1$`Cases filed by Macrism`) / sum(tab1$`Total cases`), 1),
    "%)"
  ),
  sum(tab1$`Total cases`)
)
tab1 <- as.data.frame(rbind(tab1, total))
tab1
write.table(tab1, file = paste0(figures, "table 1.txt"), row.names = FALSE, sep = ",")

# Table 2
tab2 <- dtaall %>% 
  get_summary_stats(
    acc_K_b, acc_M_b,
    acc_pol_b,
    acc_inc_b,
    acc_opp_b,
    acc_claim_K_b,
    acc_claim_M_b,
    acc_claim_pol_b,
    acc_claim_inc_b,
    acc_claim_opp_b,
    show = c("mean", "sd", "min", "max")
  )

tab2_bis <- dtaall %>% 
  select(
    acc_K,
    acc_M,
    acc_pol,
    acc_inc,
    acc_opp,
    acc_claim_K,
    acc_claim_M,
    acc_claim_pol,
    acc_claim_inc,
    acc_claim_opp
  ) %>% 
  summarize_all(.funs = "sum")

tab2 <- cbind(tab2, t(tab2_bis))

tab2$variable <- as.character(tab2$variable)
tab2$role <- ifelse(grepl("_claim", tab2$variable), "Claimant", "Defendant")
tab2$variable[grepl("acc_K", tab2$variable)] <- "CFK co-defendants"
tab2$variable[grepl("acc_claim_K", tab2$variable)] <- "CFK co-defendants"
tab2$variable[grepl("acc_M", tab2$variable)] <- "MM co-defendants"
tab2$variable[grepl("acc_claim_M", tab2$variable)] <- "MM co-defendants"
tab2$variable[grepl("acc_pol", tab2$variable)] <- "Political co-defendants"
tab2$variable[grepl("acc_inc", tab2$variable)] <- "Incumbent co-defendants"
tab2$variable[grepl("acc_opp", tab2$variable)] <- "Opposition co-defendants"
tab2$variable[grepl("acc_claim_pol", tab2$variable)] <- "Political co-defendants"
tab2$variable[grepl("acc_claim_inc", tab2$variable)] <- "Incumbent co-defendants"
tab2$variable[grepl("acc_claim_opp", tab2$variable)] <- "Opposition co-defendants"
tab2$sum <- tab2$`t(tab2_bis)`
tab2 <- tab2 %>% select(variable, role, n, mean, sd, min, max, sum)

tab2 <- as.data.frame(tab2)
write.table(tab2, file = paste0(figures, "table 2.txt"), row.names = FALSE, sep = ",")

# Figure 1
tab2 <- dtaall %>%
  select(starts_with("del"), starts_with("other"), starts_with("unknown")) 

tab2 <- tab2 %>%
  pivot_longer(
    cols = matches("^del|^other|^unknown"),
    names_to = c(".value", "status"),
    names_pattern = "(.*)_(.*)"
  )

tab2 <- tab2 %>% 
  group_by(status) %>% 
  summarise_all(list(mean = ~mean(., na.rm = TRUE), sum = ~sum(., na.rm = TRUE))) %>%
  pivot_longer(-status, names_to = "key", values_to = "value") %>%
  spread(status, value)

tab2 <- tab2 %>%
  separate(key, into = c("key", "stat")) %>%
  pivot_wider(
    names_from = stat,
    values_from = c(incumbent, opposition),
    names_glue = "{.value}_{stat}"
  )

tab2$incumbent_mean <- tab2$incumbent_mean * 100
tab2$opposition_mean <- tab2$opposition_mean * 100

# Mapping key values to descriptive labels
tab2$key[tab2$key == "del1"] <- "Abuse of power"
tab2$key[tab2$key == "del2"] <- "Fraud"
tab2$key[tab2$key == "del3"] <- "Failure to comply with public duties"
tab2$key[tab2$key == "del4"] <- "Conflict of interests"
tab2$key[tab2$key == "del5"] <- "Embezzlement"
tab2$key[tab2$key == "del6"] <- "Criminal conspiracy"
tab2$key[tab2$key == "del7"] <- "Unlawful appointment"
tab2$key[tab2$key == "del8"] <- "Bribery"
tab2$key[tab2$key == "del9"] <- "Money laundering"
tab2$key[tab2$key == "unknown"] <- "Not specified"
tab2$key[tab2$key == "other"] <- "Other"

# Calculating the total and selecting required columns
tab2$total <- tab2$incumbent_sum + tab2$opposition_sum 
tab2 <- tab2[order(tab2$total, decreasing = TRUE),]
tab2$total <- NULL
tab2 <- tab2[, c("key", "incumbent_sum", "incumbent_mean", "opposition_sum", "opposition_mean")]

# Renaming the columns
names(tab2) <- c("Crime", "Incumbent (sum)", "Incumbent (%)", "Opposition (sum)", "Opposition (%)")

# Rounding the values
tab2[, -1] <- round(tab2[, -1], 2)

p1 <- tab2 %>% select(1,3,5) %>% 
  pivot_longer(!Crime, names_to = "group", values_to = "share") %>% 
  ggplot(aes(x=Crime, y=share, fill=as.factor(group))) + 
  geom_bar(position="dodge", stat="identity") + 
  theme_minimal() + 
  ylab("Percentage") +
  xlab('Types of crimes') +
  scale_color_manual(values=c("black", "red")) + 
  labs(color  = "", linetype = "") + 
  coord_flip() +  
  theme(plot.title = element_text(hjust = 0.5), legend.position="bottom")   +
  scale_fill_manual(name = "Accusations againt:", labels = c("Incumbent", "Opposition"), limits=c("Incumbent (%)", "Opposition (%)"), values = c(col_bin[1], col_bin[3])) 

pdf(paste0(figures, "Figure 1.pdf"), width = 6, height = 6)
p1
dev.off()

# Figure 2 
# Defendants
sample <- c("acc_pol_b", "acc_inc_b", "acc_opp_b")  

# Create an empty data frame to store the results
final <- data.frame(bw = NULL, est = NULL, cilo = NULL, cihi = NULL, sample = NULL)

# Loop through each sample
for (i in sample) {

  dtaall$depvar <- unlist(dtaall[, i])
  dtaall$treat <- unlist(ifelse(dtaall$run_closest > 0, 1, 0))
  
  bw <- seq(bw_min, bw_max, by = 1)
  tmp <- data.frame(bw = bw, est = NA, cilo = NA, cihi = NA, sample = NA)
  
  # Loop through each bandwidth value
  for (j in bw) {
    
    robust_all <- lm_robust(depvar ~ treat, data = dtaall, subset = abs(run_closest) < j)
    
    tmp$est[tmp$bw == j] <- robust_all$coefficients[2]
    tmp$cilo[tmp$bw == j] <- robust_all$conf.low[2]
    tmp$cihi[tmp$bw == j] <- robust_all$conf.high[2]
    tmp$nobs[tmp$bw == j] <- robust_all$nobs
    tmp$sample[tmp$bw == j] <- ifelse(grepl("pol", i), "Incumbent & Opposition",
                                      ifelse(grepl("inc", i), "Incumbent", "Opposition"))
    tmp$sample <- factor(tmp$sample, levels = c('Incumbent & Opposition', 'Incumbent', 'Opposition'))
  }
  
  # Append the results for the current sample to the final data frame
  final <- rbind(final, tmp)
}

pp1 <- ggplot(final, aes(x = bw, y = est, color = sample)) +
  geom_hline(yintercept = 0, lty = 2, lwd = .5, colour = "grey50") +
  geom_point(size = 2) +
  theme_minimal() +
  scale_color_manual(name = "", labels = c("Co-defendants"), values = c(col_bin[1], col_bin[4], col_bin[5])) +
  labs(subtitle = paste0('Accusations against: \n')) +
  ylim(ylim) +
  xlab('Days before/after nearest election') +
  ylab('Estimate') +
  geom_errorbar(aes(ymin = cilo, ymax = cihi), linewidth = bar1, width = .2) +
  facet_wrap(~sample, ncol = 3) +
  scale_x_continuous(breaks = c(5, 10, 15, 20, 25, 30, 35),
                     labels = c(5, 10, 15, 20, 25, 30, 35),
                     sec.axis = dup_axis(
                       name = "Number of observations",
                       breaks = c(5, 10, 15, 20, 25, 30, 35),
                       labels = unique(final$nobs[final$bw %in% c(5, 10, 15, 20, 25, 30, 35)])
                     )) +
  theme(
    plot.subtitle = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0), face = "bold"),
    axis.title.x.bottom = element_text(margin = margin(t = 20, r = 0, b = 20, l = 0)),
    axis.text.x.bottom = element_text(hjust = 1, colour = "grey"),
    axis.text.x.top = element_text(hjust = .5, colour = "grey"),
    strip.text = element_text(face = "bold"),
    legend.position = "none"
  )

pdf(paste0(figures, "Figure 2 (panel a).pdf"), width = 9, height = 4)
pp1
dev.off()

# Claimants 
sample <- c("acc_claim_pol_b", "acc_claim_inc_b", "acc_claim_opp_b")  
final <- data.frame(bw = NULL, est = NULL, cilo = NULL, cihi = NULL, sample=NULL)

for (i in sample) {
  
  dtaall$depvar <- unlist(dtaall[,i])
  dtaall$treat <- unlist(ifelse(dtaall$run_closest>0, 1, 0))
  
  bw <- seq(bw_min,bw_max, by=1)
  tmp <- data.frame(bw = bw, est = NA, cilo = NA, cihi = NA, sample=NA)
  
  for(j in bw){
    
    robust_all <- lm_robust(depvar~treat, data = dtaall, subset = abs(run_closest)<j)
    
    tmp$est[tmp$bw == j] <- robust_all$coefficients[2]
    tmp$cilo[tmp$bw == j] <- robust_all$conf.low[2]
    tmp$cihi[tmp$bw == j] <- robust_all$conf.high[2]
    tmp$nobs[tmp$bw == j] <- robust_all$nobs
    tmp$sample[tmp$bw == j] <- ifelse(grepl("claim_pol", i), "Incumbent & Opposition", 
                                      ifelse(grepl("claim_inc", i), "Incumbent", "Opposition"))
    tmp$sample = factor(tmp$sample, levels=c('Incumbent & Opposition','Incumbent','Opposition'))
    
    
  }
  
  final <- rbind(final,tmp)
}

pp2 <- ggplot(final, aes(x = bw, y = est, color=sample)) +
  geom_hline(yintercept = 0, lty = 2, lwd = .5, colour = "grey50") +
  geom_point(size=2) +
  theme_minimal() + 
  scale_color_manual(name = "", labels = c("Co-defendants"), values=c(col_bin[1], col_bin[4], col_bin[5])) + 
  labs(subtitle = paste0('Accusations filed by: \n')) +
  ylim(ylim) + 
  xlab('Days before/after nearest election') +
  ylab('Estimate') +
  geom_errorbar(aes(ymin = cilo, ymax = cihi), linewidth = bar1, width = .2) +
  facet_wrap(~sample, ncol = 3) + 
  scale_x_continuous(breaks = c(5, 10, 15, 20, 25, 30, 35), 
                     labels = c(5, 10, 15, 20, 25, 30, 35),
                     sec.axis = dup_axis(name = "Number of observations",
                                         breaks = c(5, 10, 15, 20, 25, 30, 35),
                                         labels = unique(final$nobs[final$bw %in% 
                                                                      c(5, 10, 15, 20, 25, 30, 35)]))) +
  theme(plot.subtitle = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0), face = "bold"),
        axis.title.x.bottom = element_text(margin = margin(t = 20, r = 0, b = 20, l = 0)),
        axis.text.x.bottom = element_text(hjust = 1, colour = "grey"),
        axis.text.x.top = element_text(hjust = .5, colour = "grey"), 
        strip.text = element_text(face = "bold"),
        legend.position  = "none")

pdf(paste0(figures, "Figure 2 (panel b).pdf"), width = 9, height = 4)
pp2
dev.off() 


# Table 3
# Defendants
depvars <- c("acc_pol_b", "acc_inc_b", "acc_opp_b")
rd_results <- NULL

# Iterate over each dependent variable
for (i in 1:length(depvars)) {
  
  dtaall$outcome_tmp <- unlist(dtaall[, which(colnames(dtaall) == depvars[i])])
  nearest <- c(depvars[i], extract_rd(with(dtaall, rdrobust(y = outcome_tmp, x = run_closest, h = bw_max))))
  rd_results <- rbind(rd_results, nearest)
  
}

rd_results <- as.data.frame(rd_results)
names(rd_results) <- c("Outcome", "Est.", "95% CI", "p-val", "n control | n treat", "Bandwidth")

rd_results$Outcome[rd_results$Outcome == "acc_pol_b"] <- "Incumbent + Opposition"
rd_results$Outcome[rd_results$Outcome == "acc_inc_b"] <- "Incumbent cases"
rd_results$Outcome[rd_results$Outcome == "acc_opp_b"] <- "Opposition cases"

rd_results <- as.data.frame(rd_results)
write.table(rd_results, file = paste0(figures, "Table 3 (panel a).txt"), row.names = FALSE, sep = ",")

# Accusers
depvars <- c("acc_claim_pol_b", "acc_claim_inc_b", "acc_claim_opp_b")
rd_results <- NULL

# Iterate over each dependent variable
for (i in 1:length(depvars)) {
  
  dtaall$outcome_tmp <- unlist(dtaall[, which(colnames(dtaall) == depvars[i])])
  nearest <- c(depvars[i], extract_rd(with(dtaall, rdrobust(y = outcome_tmp, x = run_closest, h = bw_max))))
  rd_results <- rbind(rd_results, nearest)
  
}

rd_results <- as.data.frame(rd_results)
names(rd_results) <- c("Outcome", "Est.", "95% CI", "p-val", "n control | n treat", "Bandwidth")

rd_results$Outcome[rd_results$Outcome == "acc_pol_claim_vs_pol_b"] <- "Incumbent + Opposition"
rd_results$Outcome[rd_results$Outcome == "acc_inc_claim_vs_opp_b"] <- "Incumbent cases"
rd_results$Outcome[rd_results$Outcome == "acc_opp_claim_vs_inc_b"] <- "Opposition cases"

rd_results <- as.data.frame(rd_results)
write.table(rd_results, file = paste0(figures, "Table 3 (panel b).txt"), row.names = FALSE, sep = ",")

# Figure 3
depvars <- c("acc_pol_b_lag", "acc_pol_b_post", 
             "officials_pol_claim_no_pol", 
             "drugtrafficking", "humantrafficking", 
             "falsifyingdoc", "falsifyingmoney")
rd_results <- NULL

# Iterate over each dependent variable
for (i in 1:length(depvars)) {
  
  dtaall$outcome_tmp <- unlist(dtaall[, which(colnames(dtaall) == depvars[i])])

  nearest <- with(dtaall, rdrobust(y = outcome_tmp, x = run_closest, h = bw_max))
  nearest <- c(depvars[i], extract_rd(nearest), nearest$ci[3, 1], nearest$ci[3, 2])
  
  rd_results <- rbind(rd_results, nearest)

  }

rd_results <- as.data.frame(rd_results)
names(rd_results) <- c("Outcome", "Est.", "95% CI", "p-val", "n control | n treat", "Bandwidth", "cilo", "cihi")
rd_results$Est. <- as.numeric(rd_results$Est.)
rd_results$cilo <- as.numeric(rd_results$cilo)
rd_results$cihi <- as.numeric(rd_results$cihi)

rd_results$Outcome[rd_results$Outcome == "network_pol_b_lag"] <- "Incumbent + Opposition (t-1)"
rd_results$Outcome[rd_results$Outcome == "network_pol_b_post"] <- "Incumbent + Opposition (t+1)"
rd_results$Outcome[rd_results$Outcome == "officials_pol_claim_no_pol"] <- "Cases filed by politicians against non-politicians"
rd_results$Outcome[rd_results$Outcome == "drugtrafficking"] <- "Drug trafficking"
rd_results$Outcome[rd_results$Outcome == "humantrafficking"] <- "Human trafficking"
rd_results$Outcome[rd_results$Outcome == "falsifyingdoc"] <- "Falsifying documents"
rd_results$Outcome[rd_results$Outcome == "falsifyingmoney"] <- "Falsifying money"

rd_results$type <- c(rep("A", 2), "B", rep("C", 4))

p1 <- ggplot(rd_results, aes(x = Outcome, y = Est., color = type)) +
  geom_hline(yintercept = 0, lty = 2, lwd = .5, colour = "grey50") +
  geom_point() +
  theme_minimal() +
  geom_errorbar(aes(ymin = cilo, ymax = cihi), size = .8, width = .2) +
  ylab('Estimate') +
  coord_flip() +
  theme(plot.title = element_text(hjust = 0.5), legend.position = "none")

pdf(paste0(figures, "Figure 3.pdf"), width = 6, height = 6)
p1
dev.off()

# Newspaper analysis
# Figure 4 
depvars <- c("either_any_01_jud", "either_any_sum_jud")  
final <- data.frame(bw = NULL, est = NULL, cilo = NULL, cihi = NULL, depvars = NULL)

# Iterate over each depvar
for (i in depvars) {
  
  newspapers$depvar <- unlist(newspapers[, i])
  newspapers$treat <- unlist(ifelse(newspapers$run_closest > 0, 1, 0))
  
  bw <- seq(bw_min, bw_max, by = 1)
  tmp <- data.frame(bw = bw, est = NA, cilo = NA, cihi = NA, depvars = NA)
  
  # Iterate over each bandwidth value
  for (j in bw) {
    
    robust_all <- lm_robust(depvar ~ treat, data = newspapers, subset = abs(run_closest) < j)
    
    tmp$est[tmp$bw == j] <- robust_all$coefficients[2]
    tmp$cilo[tmp$bw == j] <- robust_all$conf.low[2]
    tmp$cihi[tmp$bw == j] <- robust_all$conf.high[2]
    tmp$nobs[tmp$bw == j] <- robust_all$nobs
    tmp$depvars[tmp$bw == j] <- ifelse(grepl("either_any_01", i), "Any Corruption Keyword", "Number of Corruption Keywords")
    
  }
  
  final <- rbind(final, tmp)
}

pp1 <- ggplot(final, aes(x = bw, y = est, color = depvars)) +
  geom_hline(yintercept = 0, lty = 2, lwd = .5, colour = "grey50") +
  geom_point(size = 2, shape = 18) +
  theme_minimal() +
  scale_color_manual(name = "", values = c(col_bin[1], col_bin[4])) +
  xlab('Days before/after nearest election') +
  ylab('Estimate') +
  geom_errorbar(aes(ymin = cilo, ymax = cihi), size = bar1, width = .2) +
  scale_x_continuous(
    breaks = c(5, 10, 15, 20, 25, 30, 35),
    labels = c(5, 10, 15, 20, 25, 30, 35),
    sec.axis = dup_axis(
      name = "Number of observations",
      breaks = c(5, 10, 15, 20, 25, 30, 35),
      labels = unique(final$nobs[final$bw %in% c(5, 10, 15, 20, 25, 30, 35)])
    )
  ) +
  theme(
    plot.subtitle = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0), face = "bold"),
    axis.title.x.bottom = element_text(margin = margin(t = 20, r = 0, b = 20, l = 0)),
    axis.text.x.bottom = element_text(hjust = 1, colour = "grey"),
    axis.text.x.top = element_text(hjust = .5, colour = "grey"),
    strip.text = element_text(face = "bold"),
    legend.position = "none"
  ) +
  facet_wrap(~depvars, scales = "free_y")

pdf(paste0(figures, "Figure 4.pdf"), width = 9, height = 4)
pp1
dev.off()

# Table 4 
rd_results <- NULL 

# Iterate over each dependent variable
for (i in 1:length(depvars)) {
  
  newspapers$outcome_tmp <- unlist(newspapers[, which(colnames(newspapers) == depvars[i])])
  nearest <- c(depvars[i], extract_rd(with(newspapers, rdrobust(y = outcome_tmp, x = run_closest, h = bw_max))))
  rd_results <- rbind(rd_results, nearest)

  }

rd_results <- as.data.frame(rd_results)
names(rd_results) <- c("Outcome", "Est.", "95% CI", "p-val", "n control | n treat", "Bandwidth")

rd_results$Outcome[rd_results$Outcome == "either_any_sum"] <- "Any corruption keyword"
rd_results$Outcome[rd_results$Outcome == "either_any_sum_jud"] <- "Number of corruption keywords"

rd_results <- as.data.frame(rd_results)
write.table(rd_results, file = paste0(figures, "Table 4.txt"), row.names = FALSE, sep = ",")



# Appendix A 
# Table A1: Local-linear estimates

# (a): Excluding accusations against members of both parties
depvars <- c("acc_pol_robust_b", "acc_inc_robust_b", "acc_opp_robust_b")  
rd_results <- NULL 

for(i in 1:length(depvars)){
  dtaall$outcome_tmp <- unlist(dtaall[,which(colnames(dtaall) == depvars[i])])
  nearest <- c(depvars[i], extract_rd(with(dtaall, rdrobust(y=outcome_tmp, x=run_closest, h = bw_max))))
  rd_results <- rbind(rd_results,nearest)
}

rd_results <- as.data.frame(rd_results)
names(rd_results) <- c("Outcome", "Est.", "95% CI", "p-val", "n control | n treat", "Bandwidth")

rd_results$Outcome[rd_results$Outcome=="acc_pol_robust_b"] <- "Incumbent + Opposition"
rd_results$Outcome[rd_results$Outcome=="acc_inc_robust_b"] <- "Incumbent cases"
rd_results$Outcome[rd_results$Outcome=="acc_opp_robust_b"] <- "Opposition cases"

robust <- as.data.frame(rd_results); robust
row.names(robust) <- NULL

# (b): Number of cases per day
depvars <- c("acc_pol_count", "acc_inc_count", "acc_opp_count")  
rd_results <- NULL 

for(i in 1:length(depvars)){
  dtaall$outcome_tmp <- unlist(dtaall[,which(colnames(dtaall) == depvars[i])])
  nearest <- c(depvars[i], extract_rd(with(dtaall, rdrobust(y=outcome_tmp, x=run_closest, h = bw_max))))
  rd_results <- rbind(rd_results,nearest)
}

rd_results <- as.data.frame(rd_results)
names(rd_results) <- c("Outcome", "Est.", "95% CI", "p-val", "n control | n treat", "Bandwidth")

rd_results$Outcome[rd_results$Outcome=="acc_pol_count"] <- "Incumbent + Opposition"
rd_results$Outcome[rd_results$Outcome=="acc_inc_count"] <- "Incumbent cases"
rd_results$Outcome[rd_results$Outcome=="acc_opp_count"] <- "Opposition cases"

count <- as.data.frame(rd_results); count

# (c): Controlling for the party in power
depvars <- c("acc_pol_b", "acc_inc_b", "acc_opp_b")  
rd_results <- NULL 

for(i in 1:length(depvars)){
  
  dtaall$outcome_tmp <- unlist(dtaall[,which(colnames(dtaall) == depvars[i])])
  
  nearest <- c(depvars[i], extract_rd(with(dtaall, rdrobust(y=outcome_tmp, x=run_closest, covs = party_Cambiemos, h = bw_max))))
  rd_results <- rbind(rd_results,nearest)
}

rd_results <- as.data.frame(rd_results)
names(rd_results) <- c("Outcome", "Est.", "95% CI", "p-val", "n control | n treat", "Bandwidth")

rd_results$Outcome[rd_results$Outcome=="acc_pol_b"] <- "Incumbent + Opposition"
rd_results$Outcome[rd_results$Outcome=="acc_inc_b"] <- "Incumbent cases"
rd_results$Outcome[rd_results$Outcome=="acc_opp_b"] <- "Opposition cases"

fixed <- as.data.frame(rd_results); rd_results

# (d): Polynomial 
depvars <- c("acc_pol_b", "acc_inc_b", "acc_opp_b")  
rd_results <- NULL 

for(i in 1:length(depvars)){
  
  dtaall$outcome_tmp <- unlist(dtaall[,which(colnames(dtaall) == depvars[i])])
  
  nearest <- c(depvars[i], extract_rd(with(dtaall, rdrobust(y=outcome_tmp, x=run_closest, p=5))))
  rd_results <- rbind(rd_results,nearest)
}

rd_results <- as.data.frame(rd_results)
names(rd_results) <- c("Outcome", "Est.", "95% CI", "p-val", "n control | n treat", "Bandwidth")

rd_results$Outcome[rd_results$Outcome=="acc_pol_b"] <- "Incumbent + Opposition"
rd_results$Outcome[rd_results$Outcome=="acc_inc_b"] <- "Incumbent cases"
rd_results$Outcome[rd_results$Outcome=="acc_opp_b"] <- "Opposition cases"

polynomial <- as.data.frame(rd_results); polynomial

# (e): Alternative bandwidth: 100 days
depvars <- c("acc_pol_b", "acc_inc_b", "acc_opp_b")  
rd_results <- NULL 

for(i in 1:length(depvars)){
  
  dtaall$outcome_tmp <- unlist(dtaall[,which(colnames(dtaall) == depvars[i])])
  
  nearest <- c(depvars[i], extract_rd(with(dtaall, rdrobust(y=outcome_tmp, x=run_closest, covs = party_Cambiemos, h = 100))))
  rd_results <- rbind(rd_results,nearest)
}

rd_results <- as.data.frame(rd_results)
names(rd_results) <- c("Outcome", "Est.", "95% CI", "p-val", "n control | n treat", "Bandwidth")

rd_results$Outcome[rd_results$Outcome=="acc_pol_b"] <- "Incumbent + Opposition"
rd_results$Outcome[rd_results$Outcome=="acc_inc_b"] <- "Incumbent cases"
rd_results$Outcome[rd_results$Outcome=="acc_opp_b"] <- "Opposition cases"

onehundred <- as.data.frame(rd_results); onehundred

# (f): Distance to primary elections
depvars <- c("acc_pol_b", "acc_inc_b", "acc_opp_b")  
rd_results <- NULL 

for(i in 1:length(depvars)){
  
  dtaall$outcome_tmp <- unlist(dtaall[,which(colnames(dtaall) == depvars[i])])
  
  nearest <- c(depvars[i], extract_rd(with(dtaall, rdrobust(y=outcome_tmp, x=run_paso, h = bw_max))))
  rd_results <- rbind(rd_results,nearest)
}

rd_results <- as.data.frame(rd_results)
names(rd_results) <- c("Outcome", "Est.", "95% CI", "p-val", "n control | n treat", "Bandwidth")

rd_results$Outcome[rd_results$Outcome=="acc_pol_b"] <- "Incumbent + Opposition"
rd_results$Outcome[rd_results$Outcome=="acc_inc_b"] <- "Incumbent cases"
rd_results$Outcome[rd_results$Outcome=="acc_opp_b"] <- "Opposition cases"

paso <- as.data.frame(rd_results); paso

# (g): Distance to general elections
depvars <- c("acc_pol_b", "acc_inc_b", "acc_opp_b")  
rd_results <- NULL 

for(i in 1:length(depvars)){
  
  dtaall$outcome_tmp <- unlist(dtaall[,which(colnames(dtaall) == depvars[i])])
  
  nearest <- c(depvars[i], extract_rd(with(dtaall, rdrobust(y=outcome_tmp, x=run_general, h = bw_max))))
  rd_results <- rbind(rd_results,nearest)
}

rd_results <- as.data.frame(rd_results)
names(rd_results) <- c("Outcome", "Est.", "95% CI", "p-val", "n control | n treat", "Bandwidth")

rd_results$Outcome[rd_results$Outcome=="acc_pol_b"] <- "Incumbent + Opposition"
rd_results$Outcome[rd_results$Outcome=="acc_inc_b"] <- "Incumbent cases"
rd_results$Outcome[rd_results$Outcome=="acc_opp_b"] <- "Opposition cases"

general <- as.data.frame(rd_results); general

# (h): Cases against public officials
depvars <- c("officials_pol_b", "officials_inc_b", "officials_opp_b")  
rd_results <- NULL 

for(i in 1:length(depvars)){
  dtaall$outcome_tmp <- unlist(dtaall[,which(colnames(dtaall) == depvars[i])])
  nearest <- c(depvars[i], extract_rd(with(dtaall, rdrobust(y=outcome_tmp, x=run_closest, h = bw_max))))
  rd_results <- rbind(rd_results,nearest)
}

rd_results <- as.data.frame(rd_results)
names(rd_results) <- c("Outcome", "Est.", "95% CI", "p-val", "n control | n treat", "Bandwidth")

rd_results$Outcome[rd_results$Outcome=="officials_pol_b"] <- "Incumbent + Opposition"
rd_results$Outcome[rd_results$Outcome=="officials_inc_b"] <- "Incumbent cases"
rd_results$Outcome[rd_results$Outcome=="officials_opp_b"] <- "Opposition cases"

officials <- as.data.frame(rd_results); officials

# (i): Cases by public officials
depvars <- c("officials_claim_pol_b", "officials_claim_inc_b", "officials_claim_opp_b")  
rd_results <- NULL 

for(i in 1:length(depvars)){
  dtaall$outcome_tmp <- unlist(dtaall[,which(colnames(dtaall) == depvars[i])])
  nearest <- c(depvars[i], extract_rd(with(dtaall, rdrobust(y=outcome_tmp, x=run_closest, h = bw_max))))
  rd_results <- rbind(rd_results,nearest)
}

rd_results <- as.data.frame(rd_results)
names(rd_results) <- c("Outcome", "Est.", "95% CI", "p-val", "n control | n treat", "Bandwidth")

rd_results$Outcome[rd_results$Outcome=="officials_claim_pol_b"] <- "Incumbent + Opposition"
rd_results$Outcome[rd_results$Outcome=="officials_claim_inc_b"] <- "Incumbent cases"
rd_results$Outcome[rd_results$Outcome=="officials_claim_opp_b"] <- "Opposition cases"

officials_claim <- as.data.frame(rd_results); officials_claim

# Final RD table 
rd_results <- rbind(robust,
                    count,
                    fixed,
                    polynomial,
                    onehundred,
                    paso,
                    general,
                    officials,
                    officials_claim)


tempname <- "table_additional.tex"
filename <- paste0(figures, tempname)
row.names(rd_results) <- NULL
tex <- kable(rd_results, format = "latex", caption = "Local-linear estimates",
             booktabs = T,label = "table_additional", digits = 3) %>%
  kable_styling(font_size = 12) %>%
  footnote("Local-linear estimates with triangular weights. Confidence intervals and p-values are calculated using the robust procedure developed by Calonico, Cattaneo, and Titiunik (2014).",
           footnote_as_chunk=T,
           general_title="",
           threeparttable = T,
           fixed_small_size = T)
write(tex,file=filename)

# Table A2: Difference between local-linear estimates
acc_inc_b <- with(dtaall, rdrobust(acc_inc_b, run_closest, h = bw_max)); summary(acc_inc_b)
acc_opp_b <- with(dtaall, rdrobust(acc_opp_b, run_closest, h = bw_max)); summary(acc_opp_b)

diff <- list()
diff$est <- acc_inc_b$coef[1] - acc_opp_b$coef[1]
diff$se <- sqrt((acc_inc_b$se[3])^2 + (acc_opp_b$se[3])^2)
diff$ci_l <- c(diff$est - qnorm(.975)*diff$se)
diff$ci_h <- c(diff$est + qnorm(.975)*diff$se)
diff$pv <- 2*(1-pnorm(abs(diff$est/diff$se)))

diff_a <- c(as.character(round(diff$est, 3)), 
            paste0("[", round (diff$ci_l , 3), ":", round(diff$ci_h, 3), "]"), 
            as.character(round(diff$pv, 3)),
            NA,NA)

acc_claim_inc_b <- with(dtaall, rdrobust(acc_claim_inc_b, run_closest, h = bw_max)); summary(acc_claim_inc_b)
acc_claim_opp_b <- with(dtaall, rdrobust(acc_claim_opp_b, run_closest, h = bw_max)); summary(acc_claim_opp_b)

diff <- list()
diff$est <- acc_claim_inc_b$coef[1] - acc_claim_opp_b$coef[1]
diff$se <- sqrt((acc_claim_inc_b$se[3])^2 + (acc_claim_opp_b$se[3])^2)
diff$ci_l <- c(diff$est - qnorm(.975)*diff$se)
diff$ci_h <- c(diff$est + qnorm(.975)*diff$se)
diff$pv <- 2*(1-pnorm(abs(diff$est/diff$se)))

diff_p <- c(as.character(round(diff$est, 3)), 
            paste0("[", round (diff$ci_l , 3), ":", round(diff$ci_h, 3), "]"), 
            as.character(round(diff$pv, 3)),
            NA,NA)

rd_results <- rbind(
  extract_rd(acc_inc_b),
  extract_rd(acc_opp_b),
  diff_a,
  extract_rd(acc_claim_inc_b),
  extract_rd(acc_claim_opp_b), 
  diff_p
)

rd_results <- as.data.frame(rd_results); names(rd_results) <- c("Est.", "95% CI", "p-val", "n_c | n_t", "h")

tempname <- "local_diff.tex"
filename <- paste0(figures, tempname)
tex <- kable(rd_results, format = "latex", caption = "Local-linear estimates", 
             booktabs = T, label = "diff", digits = 3) %>%
  kable_styling(font_size = 12) 
write(tex,file=filename)


# Figure A1: Difference in the number of cases filed before and after the nearest election
# Table A8: Difference in days with cases before and after the nearest election (Poisson estimates)
depvars <- c("acc_pol_count", "acc_inc_count", "acc_opp_count")  
final <- data.frame(bw = NULL, est = NULL, cilo = NULL, cihi = NULL, sample=NULL)

for (i in depvars) {
  
  dtaall$depvar <- unlist(dtaall[,i])
  dtaall$treat <- unlist(ifelse(dtaall$run_closest>0, 1, 0))
  
  bw <- seq(bw_min,bw_max, by=1)
  tmp <- data.frame(bw = bw, est = NA, cilo = NA, cihi = NA, sample=NA, nobs=NA)
  
  for(j in bw){
    
    robust_all <- with(subset(dtaall, abs(run_closest)<j), glm(depvar~treat, family="poisson"))
    
    tmp$est[tmp$bw == j] <- robust_all$coefficients[2]
    tmp$cilo[tmp$bw == j] <- robust_all$coefficients[2] - qnorm(.975)*summary(robust_all)$coefficients[2, 2]
    tmp$cihi[tmp$bw == j] <- robust_all$coefficients[2] + qnorm(.975)*summary(robust_all)$coefficients[2, 2]
    tmp$nobs[tmp$bw == j] <- nobs(robust_all)
    tmp$sample[tmp$bw == j] <- ifelse(grepl("pol", i), "Incumbent & Opposition", 
                                      ifelse(grepl("inc", i), "Incumbent", "Opposition"))
    tmp$sample = factor(tmp$sample, levels=c('Incumbent & Opposition','Incumbent','Opposition'))
  }
  
  final <- rbind(final,tmp)
}

pp1 <- ggplot(final, aes(x = bw, y = est, color=sample)) +
  geom_hline(yintercept = 0, lty = 2, lwd = .5, colour = "grey50") +
  geom_point(size=2) +
  theme_minimal() + 
  scale_color_manual(name = "", labels = c("Co-defendants"), values=c(col_bin[1], col_bin[4], col_bin[5])) + 
  labs(title = paste0('Poisson estimates')) +
  xlab('Days before/after general election') +
  ylab('Estimate') +
  geom_errorbar(aes(ymin = cilo, ymax = cihi), size = bar1, width = .2) +
  facet_wrap(~sample) + 
  theme(plot.title = element_text(hjust = 0.5), legend.position  = "none") 

pdf(paste0(figures, "poisson.pdf"), width = 9, height = 3)
pp1
dev.off()

final <- final %>% 
  pivot_wider(names_from = sample,  values_from = c(est,cilo,cihi))

tempname <- "table_poisson.tex"
filename <- paste0(figures, tempname)
tex <- kable(final, format = "latex", caption = "Difference in days with cases before and after the nearest election (Poisson estimates)", 
             booktabs = T,label = "poisson", digits = 3) %>%
  kable_styling(font_size = 10) 
write(tex,file=filename)

# Figure A2: Corruption News Coverage before and after the election
depvars <- c("clarin_any_01_jud", "clarin_any_sum_jud","pagina_any_01_jud", "pagina_any_sum_jud")  
final <- data.frame(bw = NULL, est = NULL, cilo = NULL, cihi = NULL, sample=NULL)

for (i in depvars) {
  
  newspapers$depvar <- unlist(newspapers[,i])
  newspapers$treat <- unlist(ifelse(newspapers$run_closest>0, 1, 0))
  
  bw <- seq(bw_min,bw_max, by=1)
  tmp <- data.frame(bw = bw, est = NA, cilo = NA, cihi = NA, sample=NA)
  
  for(j in bw){
    
    robust_all <- lm_robust(depvar~treat, data = newspapers, subset = abs(run_closest)<j)
    
    tmp$est[tmp$bw == j] <- robust_all$coefficients[2]
    tmp$cilo[tmp$bw == j] <- robust_all$conf.low[2]
    tmp$cihi[tmp$bw == j] <- robust_all$conf.high[2]
    tmp$sample[tmp$bw == j] <- ifelse(grepl("clarin", i), "Clarin","Pagina 12")
    tmp$type[tmp$bw == j] <- ifelse(grepl("01", i), "Any keyword","Number of keywords")
    
  }
  
  final <- rbind(final,tmp)
}

pp1 <- ggplot(final, aes(x = bw, y = est, color=sample)) +
  geom_hline(yintercept = 0, lty = 2, lwd = .5, colour = "grey50") +
  geom_point(size=2, shape = 18) +
  theme_minimal() + 
  scale_color_manual(name = "", values=c(col_bin[5],col_bin[1])) + 
  xlab('Days before/after nearest election') +
  ylab('Estimate') +
  geom_errorbar(aes(ymin = cilo, ymax = cihi), size = bar1, width = .2) +
  facet_wrap(~sample+type, scale="free") + 
  theme(plot.title = element_text(hjust = 0.5), legend.position  = "none") 

tempname <- "fig_newspapers.pdf"
filename <- paste0(figures, tempname)
pdf(filename, width = 6, height = 6)
pp1
dev.off()


# Appendix B 
# Table A3: Crimes under investigation
tab2 <- dtaall %>%
  select(starts_with("del"), starts_with("other"), starts_with("unknown")) 

tab2 <- tab2 %>%
  pivot_longer(
    cols = matches("^del|^other|^unknown"),
    names_to = c(".value", "status"),
    names_pattern = "(.*)_(.*)"
  )

tab2 <- tab2 %>% 
  group_by(status) %>% 
  summarise_all(list(mean = ~mean(., na.rm = TRUE), sum = ~sum(., na.rm = TRUE))) %>%
  pivot_longer(-status, names_to = "key", values_to = "value") %>%
  spread(status, value)

tab2 <- tab2 %>%
  separate(key, into = c("key", "stat")) %>%
  pivot_wider(
    names_from = stat,
    values_from = c(incumbent, opposition),
    names_glue = "{.value}_{stat}"
  )

tab2$incumbent_mean <- tab2$incumbent_mean * 100
tab2$opposition_mean <- tab2$opposition_mean * 100

# Mapping key values to descriptive labels
tab2$key[tab2$key == "del1"] <- "Abuse of power"
tab2$key[tab2$key == "del2"] <- "Fraud"
tab2$key[tab2$key == "del3"] <- "Failure to comply with public duties"
tab2$key[tab2$key == "del4"] <- "Conflict of interests"
tab2$key[tab2$key == "del5"] <- "Embezzlement"
tab2$key[tab2$key == "del6"] <- "Criminal conspiracy"
tab2$key[tab2$key == "del7"] <- "Unlawful appointment"
tab2$key[tab2$key == "del8"] <- "Bribery"
tab2$key[tab2$key == "del9"] <- "Money laundering"
tab2$key[tab2$key == "unknown"] <- "Not specified"
tab2$key[tab2$key == "other"] <- "Other"

# Calculating the total and selecting required columns
tab2$total <- tab2$incumbent_sum + tab2$opposition_sum 
tab2 <- tab2[order(tab2$total, decreasing = TRUE),]
tab2$total <- NULL
tab2 <- tab2[, c("key", "incumbent_sum", "incumbent_mean", "opposition_sum", "opposition_mean")]

# Renaming the columns
names(tab2) <- c("Crime", "Incumbent (sum)", "Incumbent (%)", "Opposition (sum)", "Opposition (%)")

# Rounding the values
tab2[, -1] <- round(tab2[, -1], 2)
write.table(as.data.frame(tab2), file=paste0(figures, "tab2_withmeans.txt"), row.names = F, sep=",")


# Figure 2 
depvars <- c("acc_pol_b", "acc_inc_b", "acc_opp_b")  
final <- data.frame(bw = NULL, est = NULL, cilo = NULL, cihi = NULL, sample=NULL)

for (i in depvars) {
  
  dtaall$depvar <- unlist(dtaall[,i])
  dtaall$treat <- unlist(ifelse(dtaall$run_closest>0, 1, 0))
  
  bw <- seq(bw_min,bw_max, by=1)
  tmp <- data.frame(bw = bw, est = NA, cilo = NA, cihi = NA, sample=NA, nobs=NA)
  
  for(j in bw){
    robust_all <- lm_robust(depvar~treat, data = dtaall, subset = abs(run_closest)<j)
    
    tmp$est[tmp$bw == j] <- robust_all$coefficients[2]
    tmp$cilo[tmp$bw == j] <- robust_all$conf.low[2]
    tmp$cihi[tmp$bw == j] <- robust_all$conf.high[2]
    tmp$nobs[tmp$bw == j] <- robust_all$nobs[1]
    
    tmp$sample[tmp$bw == j] <- ifelse(grepl("pol", i), "Incumbent & Opposition", 
                                      ifelse(grepl("inc", i), "Incumbent", "Opposition"))
    tmp$sample = factor(tmp$sample, levels=c('Incumbent & Opposition','Incumbent','Opposition'))
    
  }
  
  final <- rbind(final,tmp)
}

final$type <- "Defendant"
final_accusants <- final

# Claimants 
depvars <- c("acc_claim_pol_b", "acc_claim_inc_b", "acc_claim_opp_b") 
final <- data.frame(bw = NULL, est = NULL, cilo = NULL, cihi = NULL, sample=NULL)

for (i in depvars) {
  
  dtaall$depvar <- unlist(dtaall[,i])
  dtaall$treat <- unlist(ifelse(dtaall$run_closest>0, 1, 0))
  
  bw <- seq(bw_min,bw_max, by=1)
  tmp <- data.frame(bw = bw, est = NA, cilo = NA, cihi = NA, sample=NA, nobs=NA)
  
  for(j in bw){
    robust_all <- lm_robust(depvar~treat, data = dtaall, subset = abs(run_closest)<j)
    
    tmp$est[tmp$bw == j] <- robust_all$coefficients[2]
    tmp$cilo[tmp$bw == j] <- robust_all$conf.low[2]
    tmp$cihi[tmp$bw == j] <- robust_all$conf.high[2]
    tmp$nobs[tmp$bw == j] <- robust_all$nobs[1]
    
    tmp$sample[tmp$bw == j] <- ifelse(grepl("claim_pol", i), "Incumbent & Opposition", 
                                      ifelse(grepl("claim_inc", i), "Incumbent", "Opposition"))
    tmp$sample = factor(tmp$sample, levels=c('Incumbent & Opposition','Incumbent','Opposition'))
    
  }
  
  final <- rbind(final,tmp)
}

final$type <- "Accusant"
final <- rbind(final_accusants, final)

final <- final %>% 
  pivot_wider(names_from = type,  values_from = c(est,cilo,cihi)) %>%
  select(bw,sample,nobs,est_Defendant,cilo_Defendant,cihi_Defendant,est_Accusant,cilo_Accusant,cihi_Accusant)
names(final) <- c("Window", "Outcome", "N", "B_d", "95% CI_d (Low)", "95% CI_d (High)", "B_a", "95% CI_a (Low)", "95% CI_a (High)" )

all <- final %>% subset(Outcome=="Incumbent & Opposition")  
inc <- final %>% subset(Outcome=="Incumbent")  
opp <- final %>% subset(Outcome=="Opposition")  

# Table A4: Difference in days with cases before and after the nearest election (Incumbent and Opposition)
tempname <- "figure_2_table_all.tex"
filename <- paste0(figures, tempname)
tex <- kable(all, format = "latex", caption = "Difference in days with cases before and after the nearest election (Incumbent and Opposition)", 
             booktabs = T,label = "all", digits = 3) %>%
  kable_styling(font_size = 10) 
write(tex,file=filename)

# Table A5: Difference in days with cases before and after the nearest election (Incumbent)
tempname <- "figure_2_table_inc.tex"
filename <- paste0(figures, tempname)
tex <- kable(inc, format = "latex", caption = "Difference in days with cases before and after the nearest election (Incumbent)", 
             booktabs = T,label = "inc", digits = 3) %>%
  kable_styling(font_size = 10) 
write(tex,file=filename)

# Table A6: Difference in days with cases before and after the nearest election (Opposition)
tempname <- "figure_2_table_opp.tex"
filename <- paste0(figures, tempname)
tex <- kable(opp, format = "latex", caption = "Difference in days with cases before and after the nearest election (Opposition)", 
             booktabs = T,label = "opp", digits = 3) %>%
  kable_styling(font_size = 10) 
write(tex,file=filename)

# Table A8: Difference in days with cases before and after the nearest election (Poisson estimates)
depvars <- c("acc_pol_count", "acc_inc_count", "acc_opp_count")  
final <- data.frame(bw = NULL, est = NULL, cilo = NULL, cihi = NULL, sample=NULL)

for (i in depvars) {
  
  dtaall$depvar <- unlist(dtaall[,i])
  dtaall$treat <- unlist(ifelse(dtaall$run_closest>0, 1, 0))
  
  bw <- seq(bw_min,bw_max, by=1)
  tmp <- data.frame(bw = bw, est = NA, cilo = NA, cihi = NA, sample=NA, nobs=NA)
  
  for(j in bw){
    
    robust_all <- with(subset(dtaall, abs(run_closest)<j), glm(depvar~treat, family="poisson"))
    
    tmp$est[tmp$bw == j] <- robust_all$coefficients[2]
    tmp$cilo[tmp$bw == j] <- robust_all$coefficients[2] - qnorm(.975)*summary(robust_all)$coefficients[2, 2]
    tmp$cihi[tmp$bw == j] <- robust_all$coefficients[2] + qnorm(.975)*summary(robust_all)$coefficients[2, 2]
    tmp$nobs[tmp$bw == j] <- nobs(robust_all)
    tmp$sample[tmp$bw == j] <- ifelse(grepl("pol", i), "Incumbent & Opposition", 
                                      ifelse(grepl("inc", i), "Incumbent", "Opposition"))
    tmp$sample = factor(tmp$sample, levels=c('Incumbent & Opposition','Incumbent','Opposition'))
  }
  
  final <- rbind(final,tmp)
}

final <- final %>% 
  pivot_wider(names_from = sample,  values_from = c(est,cilo,cihi))

tempname <- "table_poisson.tex"
filename <- paste0(figures, tempname)
tex <- kable(final, format = "latex", caption = "Difference in days with cases before and after the nearest election (Poisson estimates)", 
             booktabs = T,label = "poisson", digits = 3) %>%
  kable_styling(font_size = 10) 
write(tex,file=filename)

# Table A7: Placebo tests 
depvars <- c("acc_pol_b_lag", "acc_pol_b_post", 
             "officials_pol_claim_no_pol", 
             "drugtrafficking", "humantrafficking", 
             "falsifyingdoc", "falsifyingmoney")  

rd_results <- NULL 

for(i in 1:length(depvars)){
  
  print(depvars[i])
  
  dtaall$outcome_tmp <- unlist(dtaall[,which(colnames(dtaall) == depvars[i])])
  nearest <- with(dtaall, rdrobust(y=outcome_tmp, x=run_closest, h = bw_max))
  
  nearest <- c(depvars[i], extract_rd(nearest)) 
  
  rd_results <- rbind(rd_results,nearest)
  
}

rd_results <- as.data.frame(rd_results)
names(rd_results) <- c("Outcome", "Est.", "95% CI", "p-val", "n control | n treat", "Bandwidth")

rd_results$Outcome[rd_results$Outcome=="network_pol_b_lag"] <- "Incumbent + Opposition (t-1)"
rd_results$Outcome[rd_results$Outcome=="network_pol_b_post"] <- "Incumbent + Opposition (t+1)"
rd_results$Outcome[rd_results$Outcome=="officials_pol_claim_no_pol"] <- "Cases filed by politicians against non-politicians"
rd_results$Outcome[rd_results$Outcome=="drugtrafficking"] <- "Drug trafficking"
rd_results$Outcome[rd_results$Outcome=="humantrafficking"] <- "Human trafficking"
rd_results$Outcome[rd_results$Outcome=="falsifyingdoc"] <- "Falsifying documents"
rd_results$Outcome[rd_results$Outcome=="falsifyingmoney"] <- "Falsifying money"

tempname <- "figure_3_table.tex"
filename <- paste0(figures, tempname)
row.names(rd_results) <- NULL 

tex <- kable(rd_results, format = "latex", caption = "Placebo tests", 
             booktabs = T,label = "figure_3_table", digits = 3) %>%
  kable_styling(font_size = 12) %>%
  footnote("Estimates come from local-linear regressions. Bars represent robust 95% confidence intervals as developed by Calonico, Cattaneo, and Titiunik (2014).",
           footnote_as_chunk=T, 
           general_title="",
           threeparttable = T, 
           fixed_small_size = T)
write(tex,file=filename)


# Appendix C 
# Election dates 
elect <- elections %>% 
  select(election_date,election_type) %>% 
  rename(Date = election_date, Type =election_type)

tempname <- "elections.tex"
filename <- paste0(figures, tempname)
tex <- kable(elect, format = "latex", caption = "Election dates", 
             booktabs = T,label = "elect") %>%
  kable_styling(font_size = 12) 
write(tex,file=filename)
